package edu.colorado.karl.mainsched;


import java.util.ArrayList;

import edu.colorado.karl.databases.ChangeSetpoint;
import edu.colorado.karl.databases.SPResponse;
import edu.colorado.karl.databases.SetpointsDatabase;
import edu.colorado.karl.intelsched.ISInfo;
import edu.colorado.karl.intelsched.ISInfoBuilder;
import edu.colorado.karl.intelsched.SPData;
import edu.colorado.karl.intelsched.SetPointEvaluator;

/**
 * Decides which set points to use and updates WebCTRL accordingly
 * @author boehmap
 *
 */
public class ModeSwitch {
	private SetPointEvaluator spe;
	
	
	/**
	 * Creates new empty ModeSwitch object
	 *
	 */
	public ModeSwitch() {
		spe = new SetPointEvaluator(null, null);
	}

	
	/**
	 * Examines the current data and decides which set points to use.
	 *
	 */
	public void run() {
		//	Create a new ISInfo
		ISInfo newInfo = null;
		newInfo = ISInfoBuilder.buildISInfo();
		
		//	Update the IS with ISInfo
		spe.updateISinfo(newInfo);
		
		//	Update the weather
		spe.updateWeather();
		
		//	Get Set Points
		SPData spd = spe.getSetPoint();

		//	Save Set Points
		SetpointsDatabase spDB = new SetpointsDatabase();
		
		spDB.updateSetPoint(SetpointsDatabase.AUTO_HOT_TANK, 
				spd.getHotTankSetpoint());
		spDB.updateSetPoint(SetpointsDatabase.AUTO_COLD_TANK,
				spd.getHotTankSetpoint());
		
		ArrayList<SPResponse> spList = spDB.getTankSetPoints();
		
		//	Decide automatic vs manual
		if(spDB.getMode()){
			for (SPResponse response : spList) {
			if(response.getName().equals(SetpointsDatabase.AUTO_HOT_TANK))
				ChangeSetpoint.updateSetpoint(SetpointsDatabase.AUTO_HOT_TANK,
						response.getValue());
			else if(response.getName().equals(SetpointsDatabase.AUTO_COLD_TANK))
				ChangeSetpoint.updateSetpoint(SetpointsDatabase.AUTO_COLD_TANK,
						response.getValue());
			}
		}
		else{
			for (SPResponse response : spList) {
			if(response.getName().equals(SetpointsDatabase.HOT_TANK_SP))
				ChangeSetpoint.updateSetpoint(SetpointsDatabase.HOT_TANK_SP,
						response.getValue());
			else if(response.getName().equals(SetpointsDatabase.COLD_TANK_SP))
				ChangeSetpoint.updateSetpoint(SetpointsDatabase.COLD_TANK_SP,
						response.getValue());
			}
		}
	}	
}
